package com.example.shaidemo;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;

public class GongZiFragment extends Fragment {
    private XBanner xBanner;
    private ListView listView;
    private LinearLayout cha_hangye;
    private LinearLayout cha_chengshi;
    private LinearLayout cha_qiye;


    private List<String> img_list = new ArrayList<>();
    private List<Gongzi> salary_list=new ArrayList<>();

    GongziAdapter gongziAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_gong_zi,container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

                xBanner = view.findViewById(R.id.xbanner);
                listView=view.findViewById(R.id.home_gongzi_list);
                cha_chengshi=view.findViewById(R.id.cha_chengshi);
                cha_hangye=view.findViewById(R.id.cha_hangye);
                cha_qiye=view.findViewById(R.id.cha_qiye);
                cha_chengshi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), ChaChengshiActivity.class);
                        startActivity(intent);
                    }
                });
                cha_hangye.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), ChaHangyeActivity.class);
                        startActivity(intent);
                    }
                });
                cha_qiye.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), ChaQiyeActivity.class);
                        startActivity(intent);
                    }
                });
                 //加载轮播
                img_list.add("https://tse1-mm.cn.bing.net/th/id/R-C.42464f02d9f10d02a561749642ec4310?rik=9OsaOD1khFNVVA&riu=http%3a%2f%2fwww.leezon.net%2fuploads%2fnews%2f20200314160130384800.jpg&ehk=BBryPEcbs679jdXVqVf4q4J3z00aD%2b2YOmDFpMgROAQ%3d&risl=&pid=ImgRaw&r=0");
                img_list.add("https://tse3-mm.cn.bing.net/th/id/OIP-C.0PBHRxpl34VRWjqSAToCIwHaE8?pid=ImgDet&rs=1");
                img_list.add("https://tse1-mm.cn.bing.net/th/id/R-C.429d6ee87780a7f60857a703744f571f?rik=ugNIqb%2fuc1EHRQ&riu=http%3a%2f%2fwww.dxgb-edu.cn%2fuploads%2fallimg%2f190729%2f1-1ZH91520462H.jpg&ehk=o8rYXy%2fYrG9QYsiCwrxdfoPOdGfQ6yGKRvlpar4Z7%2bY%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1");
                img_list.add("https://tse1-mm.cn.bing.net/th/id/R-C.64d7ba4b8ee84fb49221215e19a192d6?rik=jvWH8f0zNwwhXw&riu=http%3a%2f%2fi0.hdslb.com%2fbfs%2farchive%2ff8a9a36762520cff9d83fad97fd8fb442be14186.jpg&ehk=m3IqZKdzd1eC7MmxPTL9xi%2bBnv2VQ1v0ucIvO6LPQNs%3d&risl=&pid=ImgRaw&r=0");
                img_list.add("https://www.itingwa.com/file/img/2017-02/20170215122151-MjU5ODkw_500x304.jpg");

                xBanner.setData(img_list,null);

                xBanner.loadImage(new XBanner.XBannerAdapter() {
                    @Override
                    public void loadBanner(XBanner banner, Object model, View view, int position) {
                        RequestOptions myOptions = new RequestOptions()
                                .transform(new GlideRoundTransform(getContext(),10));

                        Glide
                                .with(getContext())
                                .load(img_list.get(position))
                                .apply(myOptions)
                                .into((ImageView) view);

                    }
                });
                //加载工资列表

            salary_list.add(0,new Gongzi(1,"保险顾问","销售","金融销售","6833","56","3","深圳","中国平安证券"));
            salary_list.add(1,new Gongzi(2,"JAVA","技术","后端开发","15780","436","5","成都","中软国际"));
            salary_list.add(2,new Gongzi(3,"Web前端","技术","前端开发","17900","1154","2","杭州","杭州阿里巴巴网络科技有限公司"));
            salary_list.add(3,new Gongzi(4,"产品经理","产品","产品经理","31600","339","9","北京","百度"));
            salary_list.add(4,new Gongzi(5,"JAVA","技术","后端开发","47300","2158","10","上海","蚂蚁金服网络技术有限公司"));

        gongziAdapter = new GongziAdapter(getContext(), R.layout.list_item_gongzi_shouye, salary_list);
        listView.setAdapter(gongziAdapter);
        setListViewHeightBasedOnChildren(listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到详情页
                Intent intent = new Intent(getActivity(), GongziDetailActivity.class);
                startActivity(intent);
            }
        });



    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
    }
